package com.xmkj.bearuser;

import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.widget.tablayout.BottomTabLayout;
import com.common.widget.tablayout.CustomTabEntity;
import com.common.widget.tablayout.OnTabSelectListener;
import com.common.widget.tablayout.TabEntity;
import com.xmkj.bearuser.home.HomeFragment;
import com.xmkj.bearuser.membercenter.MemberCenterFragment;
import com.xmkj.bearuser.sort.SortDetailsFrament;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/6/12
 * 描述：
 */

public class MainBearBizActivity extends BaseMvpActivity {

    @BindView(R.id.fragment)
    FrameLayout fragment;
    @BindView(R.id.tab_layout)
    BottomTabLayout tabLayout;
    private String[] mTitles;
    private ArrayList<Fragment> fragments;
    //模块未选中图片
    private int[] mIconUnSelectIds = {R.mipmap.home_enable, R.mipmap.sort_enable, R.mipmap.constum_enable,R.mipmap.member_enable};
    //模块选中图片
    private int[] mIconSelectIds = {R.mipmap.home, R.mipmap.sort, R.mipmap.constum,R.mipmap.menber};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int currentPosition;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_win;
    }

    @Override
    protected void setStatusBar() {
//        StatusBarUtil.setStatusBarColor(this, R.color.black);
    }

    @Override
    protected void setNavigation() {
        super.setNavigation();
        m_statusBar.setVisibility(View.GONE);

    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        mTitles = new String[]{"首页", "分类", "消费记录","会员中心"};
        initTabLayout();
    }

    /**
     * 按两次返回键退出
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToastMsg("再按一次退出" + getString(R.string.app_name));
                exitTime = System.currentTimeMillis();
                return true;
            } else {
                onBackPressed();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    private void initTabLayout() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new SortDetailsFrament());
        fragments.add(new ConSumFragment());
        fragments.add(new MemberCenterFragment());
        tabLayout.setTabData(mTabEntities, this, R.id.fragment, fragments);
        tabLayout.onRestoreInstanceState(savedInstanceState);

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                System.out.println("position=="+position);
         /*       if(currentPosition!=position){
                    if(position==3||position==2){
                        if (EmptyUtils.isEmpty(SPUtils.getShareString(Constans.HASHID))) {  // 没登录
                            tabLayout.setCurrentTab(currentPosition);
                            gotoActivity(LoginActivtiy.class);
                        }
                    }
                }*/
                currentPosition = position;

            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }
}
